package cn.gzsf.springboot03.controller;


import cn.gzsf.springboot03.entity.Emp;
import cn.gzsf.springboot03.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//@Controller
@RestController  //此注解作用:在此类的每个方法上添加@ResponseBody
public class EmpController {

    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        try {
            Connection conn = DBUtils.getConn();
            String sql = "insert into myemp (id,name,job,sal) values (null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "添加完成";
    }

    @RequestMapping("/select")
    public String select(){
        List<Emp> list = new ArrayList<>();//集合用于封装员工对象
        try {
            Connection conn = DBUtils.getConn();
            String sql = "select * from myemp";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int sal = rs.getInt(3);
                String job = rs.getString(4);
                Emp emp = new Emp(id,name,sal,job);
                list.add(emp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String html = "<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><td>id</td><td>名字</td><td>工作</td><td>工资</td><td>操作</td></tr>";
        //添加多行员工信息
        for(Emp e:list){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        try {
            Connection conn = DBUtils.getConn();
            String sql = "update myemp set name=?,job=?,sal=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "修改完成！<a href='/select'>返回员工列表</a>";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        try {
            Connection conn = DBUtils.getConn();
            String sql = "delete from myemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "删除完成！<a href='/select'>返回员工列表</a>";
    }

}
